package com.school.base.service;

import java.util.List;

import org.apache.log4j.Logger;

import com.school.base.dao.ISubjectDao;
import com.school.base.model.Subject;
import com.school.common.tags.PageBean;

public class SubjectService {
	private static Logger logger = Logger.getLogger(SubjectService.class);
	private ISubjectDao subjectDao;

	public ISubjectDao getSubjectDao() {
		return subjectDao;
	}

	public void setSubjectDao(ISubjectDao subjectDao) {
		this.subjectDao = subjectDao;
	}

	public Subject getSubjectById(int id) {
		return subjectDao.getSubjectById(id);
	}

	public List<Subject> getSubjectList(Subject subject, PageBean pageBean) {
		if (pageBean == null) {
			pageBean = new PageBean();
		}
		pageBean.setTotalRecordCount(subjectDao.getTotalCount(subject));
		return subjectDao.getSubjectList(subject, pageBean.getOffset(), pageBean.getPageSize());
	}

	private void validateOption(Subject subject) throws Exception {
		if (subject == null) {
			logger.error("the subject object is empty.");
			throw new Exception("科目对象为空");
		}
		if (subject.getName() == null || subject.getName().isEmpty()) {
			logger.error("the subject's name is empty.");
			throw new Exception("科目名称不能为空");
		}
	}

	public void updateSubject(Subject subject) throws Exception {
		this.validateOption(subject);
		subjectDao.updateSubject(subject);
	}

	public void deleteSubject(int id) throws Exception {
		subjectDao.deleteSubject(id);
	}

	public void insertSubject(Subject subject) throws Exception {
		this.validateOption(subject);
		subjectDao.insertSubject(subject);
	}

	public List<Subject> getAll() {
		return subjectDao.getAll();
	}
}
